package com.sz.wu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sz.wu.mapper.UserMapper;
import com.sz.wu.model.PageBean;
import com.sz.wu.model.User;
import com.sz.wu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageBean<User> showAll(int pageNum, int pageSize) {
//        开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<User> hlist = userMapper.showAll();
        Page<User> plist = (Page<User>) hlist;
        PageBean<User> pb = new PageBean<>(
                plist.getTotal(),
                plist.getResult(),
                plist.getPageSize(),
                plist.getPages(),
                plist.getPageNum()
        );
        return pb;
    }


    @Override
    public  PageBean<User> Like(int pageNum,int pageSize,String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.Like("%"+name+"%");
        Page<User> plist = (Page<User>) list;
        PageBean<User> pb = new PageBean<>(
                plist.getTotal(),
                plist.getResult(),
                plist.getPageSize(),
                plist.getPages(),
                plist.getPageNum()
        );
        return pb;
    }

    @Override
    public void delete(int id) {
        userMapper.delete(id);
    }

    @Override
    public void add(User user) {
        userMapper.add(user);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);

    }

    @Override
    public List<User> showAll2() {
        return userMapper.showAll2();
    }
}
